$(document).ready(function(){

	$('#botonBuscar').attr("disabled", "disabled");
	
	$('select[name="Marca"]').change(function(){
		$('select[name="Modelo"]').load('modelo.php?marca='+$('option:selected',this).text(),function(){
			
			if($('select[name="Marca"] option:selected').text()=="Marca")
				if($('select[name="Modelo"] option:selected').text()=="Modelo")
					$('#botonBuscar').attr("disabled", "disabled");
				else
					$('#botonBuscar').removeAttr("disabled"); 
			else
				$('#botonBuscar').removeAttr("disabled"); 
		});
	});
	
	$('select[name="Modelo"]').change(function(){
		if($('select[name="Modelo"] option:selected').text()=="Modelo")
			$('#botonBuscar').attr("disabled", "disabled");
		else
			$('#botonBuscar').removeAttr("disabled"); 
	});
	
	oyentes();
	
	//elegir uno de los dos codigos siguientes para ver las imagenes mas grandes
	/*$('.scroll-content-item').click(function(){

		var parent=$(this).parent();
		var ident=$('.idFoto',parent).val();
		tamanioCompleto(ident);
	});*/

	Shadowbox.init({overlayOpacity: 0.8, continuous:     true});
	
	$('td.connect_widget_button_cell').click(function(){
		$('h1 span').load('megusta.php',function(){
			;
		});
	});
		
	$('img.scroll-content-item').load(function(){
		var w=$(this).width();
		var h=$(this).height();
		if(w>100 | h>100)
		{
			var escala = 100.0/Math.max(w,h);
			var ancho=w*escala;
			var alto=h*escala;
			
			$(this).width(ancho);
			$(this).height(alto);
			
			w=ancho;
			h=alto;
		}
		$(this).css('margin-left',(100-w)/2);
		$(this).css('margin-top',(100-h)/2);
	});
	
	$('.descripcion').slideUp('normal');	
	$('div.acordeon:first').slideDown('normal');
	
	$('div.acordeon').click(function() {
		//$(".acordeon").show('slow');
		//$('.descripcion').slideUp('normal');	
		$(this).next().slideToggle('normal');
	});
	
	$('#com').click(function(){
		$('#info').dialog({
			height: '300',
			modal: true
		});
	
	});
	
	if($('.edicionPrincipal').length!=0)
		Initialize();
});

/*
function categoria() {/* Devuelve la categoria de la lista actual obtenida desde la URL*/
/*	var aux =location.search.split('?');
	aux = aux[1].split('&');
	var categ="";
	if(aux.length>0){
		for (var i=0;i<aux.length;i++)
		{
			var index =aux[i].indexOf("=");
			var start = aux[i].substring(0,index+1);
			if(start=="categ="){
				categ=aux[i];
				break;
			}
		}
	}
	return categ;
}*/
function oyentes(){
	$( "#siguiente" ).button({
			text: true,

		}).click(function(){
		var pag=parseInt($('#paginaHidden').val())+1;
		paginar(pag);
	});
	$( "#anterior" ).button({
			text: true,
		}).click(function(){
		var pag=parseInt($('#paginaHidden').val())-1;
		if(pag>=0){
			paginar(pag);
		}
	});
	$( "#primera" ).button({
			text: true,
		}).click(function(){
		var pag=parseInt($('#paginaHidden').val());
		if(pag>0){
			paginar(0);
		}
	});
	$( "#ultima" ).button({
		text: true,
	}).click(function(){
		paginar(-3);		
	});
	
	$('.ui-button').css('font-size','10px');
	
	$('select[name=ordenar]').change(function(){
		var c = location.search.substring(1); //saco el caracter '?'
		c=c+'&ordenar='+$('select[name=ordenar] option:selected').val();
		var pag=parseInt($('#paginaHidden').val());
		$('#contCateg').slideUp('slow',function(){			
			$(this).load('ajaxPaginador.php?pag='+pag+'&'+c, function() {
				oyentes();
				$(this).slideDown('slow');
			});
		});
	});
	
	$('select[name=cantidad]').change(function(){
		var c = location.search.substring(1); //saco el caracter '?'
		c=c+'&cantidad='+$('select[name=cantidad] option:selected').val();
		var pag=parseInt($('#paginaHidden').val());
		$('#contCateg').slideUp('slow',function(){			
			$(this).load('ajaxPaginador.php?pag='+pag+'&'+c, function() {
				oyentes();
				$(this).slideDown('slow');
			});
		});
	});
}

function paginar(pag){
	var c = location.search.substring(1);
	c=c+'&ordenar='+$('select[name=ordenar] option:selected').val();
	$('#contCateg').slideUp('slow',function(){
		$(this).load('ajaxPaginador.php?pag='+pag+'&'+c, function() {
			oyentes();
			$(this).slideDown('slow');
		});
	});
}

/* JQuery del slider de las imagenes*/
$(function() {
		//scrollpane parts
		var scrollPane = $( ".scroll-pane" ),
			scrollContent = $( ".scroll-content" );
		
		//build slider
		var scrollbar = $( ".scroll-bar" ).slider({
			slide: function( event, ui ) {
				if ( scrollContent.width() > scrollPane.width() ) {
					scrollContent.css( "margin-left", Math.round(
						ui.value / 100 * ( scrollPane.width() - scrollContent.width() )
					) + "px" );
				} else {
					scrollContent.css( "margin-left", 0 );
				}
			}
		});
		
		//append icon to handle
		var handleHelper = scrollbar.find( ".ui-slider-handle" )
		.mousedown(function() {
			scrollbar.width( handleHelper.width() );
		})
		.mouseup(function() {
			scrollbar.width( "100%" );
		})
		.append( "<span class='ui-icon ui-icon-grip-dotted-vertical'></span>" )
		.wrap( "<div class='ui-handle-helper-parent'></div>" ).parent();
		
		//change overflow to hidden now that slider handles the scrolling
		scrollPane.css( "overflow", "hidden" );
		
		//size scrollbar and handle proportionally to scroll distance
		function sizeScrollbar() {
			var remainder = scrollContent.width() - scrollPane.width();
			var proportion = remainder / scrollContent.width();
			var handleSize = scrollPane.width() - ( proportion * scrollPane.width() );
			scrollbar.find( ".ui-slider-handle" ).css({
				width: handleSize,
				"margin-left": -handleSize / 2
			});
			handleHelper.width( "" ).width( scrollbar.width() - handleSize );
		}
		
		//reset slider value based on scroll content position
		function resetValue() {
			var remainder = scrollPane.width() - scrollContent.width();
			var leftVal = scrollContent.css( "margin-left" ) === "auto" ? 0 :
				parseInt( scrollContent.css( "margin-left" ) );
			var percentage = Math.round( leftVal / remainder * 100 );
			scrollbar.slider( "value", percentage );
		}
		
		//if the slider is 100% and window gets larger, reveal content
		function reflowContent() {
				var showing = scrollContent.width() + parseInt( scrollContent.css( "margin-left" ), 10 );
				var gap = scrollPane.width() - showing;
				if ( gap > 0 ) {
					scrollContent.css( "margin-left", parseInt( scrollContent.css( "margin-left" ), 10 ) + gap );
				}
		}
		
		//change handle position on window resize
		$( window ).resize(function() {
			resetValue();
			sizeScrollbar();
			reflowContent();

		});
		//init scrollbar size
		setTimeout( sizeScrollbar, 10 );//safari wants a timeout
});


/* JQuery del combo box editable*/


function tamanioCompleto(id) {
	// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
//	$( "#dialog:ui-dialog" ).dialog( "destroy" );
	if(id!=-1){
		$( "#dialog-modal" ).dialog({
			height: 'auto',
			modal: true
		});
		
		$( "#dialog-modal :first-child" ).remove();
		$( "#dialog-modal").append('<img src="img/spinner2.gif"/>');
		var width = $('div.ui-dialog').width();
		$('div.ui-dialog').css('left',(screen.width-width)/2);
		$('div.ui-dialog').css('top','50px');
		$('div.ui-dialog img').css('margin-left',($( "#dialog-modal" ).width()-80)/2);
			
		$( "#dialog-modal" ).load('imagen.php?id='+id, function() {
			width = $('div.ui-dialog').width();
			$('div.ui-dialog').css('left',(screen.width-width)/2);
			$('div.ui-dialog').css('top','50px');
		});
	}
}

function Initialize()
{

	new HrvToolkit.Utilidades.ComboEditable('selectboxMarca');
	new HrvToolkit.Utilidades.ComboEditable('selectboxModelo');
	new HrvToolkit.Utilidades.ComboEditable('selectboxCategoria');
	new HrvToolkit.Utilidades.ComboEditable('selectboxPrecio');
	
	new HrvToolkit.Utilidades.ComboEditable('selectboxMotor');
	new HrvToolkit.Utilidades.ComboEditable('selectboxCilindrada');
	new HrvToolkit.Utilidades.ComboEditable('selectboxDiametro');
	new HrvToolkit.Utilidades.ComboEditable('selectboxCompresion');
	new HrvToolkit.Utilidades.ComboEditable('selectboxAlimentacion');
	new HrvToolkit.Utilidades.ComboEditable('selectboxEmbrague');
	new HrvToolkit.Utilidades.ComboEditable('selectboxTransmision');
	new HrvToolkit.Utilidades.ComboEditable('selectboxArranque');
	
	new HrvToolkit.Utilidades.ComboEditable('selectboxChasis');
	new HrvToolkit.Utilidades.ComboEditable('selectboxSuspensionDelantera');
	new HrvToolkit.Utilidades.ComboEditable('selectboxSuspensionTrasera');
	new HrvToolkit.Utilidades.ComboEditable('selectboxFrenoDelantero');
	new HrvToolkit.Utilidades.ComboEditable('selectboxFrenoTrasero');
	new HrvToolkit.Utilidades.ComboEditable('selectboxNeumaticoDelantero');
	new HrvToolkit.Utilidades.ComboEditable('selectboxNeumaticoTrasero');
	
	new HrvToolkit.Utilidades.ComboEditable('selectboxLongitud');
	new HrvToolkit.Utilidades.ComboEditable('selectboxAncho');
	new HrvToolkit.Utilidades.ComboEditable('selectboxAlto');
	new HrvToolkit.Utilidades.ComboEditable('selectboxAlturaAsiento');
	new HrvToolkit.Utilidades.ComboEditable('selectboxEjes');
	new HrvToolkit.Utilidades.ComboEditable('selectboxDistaciaSuelo');
	new HrvToolkit.Utilidades.ComboEditable('selectboxCombustible');

	
}
function activar(){
  document.getElement.ById("url").disabled = false;
}

